package com.xywl.escort.view.order.purchase.payment;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.LogisticsResp;
import com.xywl.escort.bean.PurchaseOrderListReq;
import com.xywl.escort.bean.PurchaseOrderListResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class PendingPaymentOrderPresenter implements PendingPaymentOrderContract.Presenter {
    private PendingPaymentOrderContract.View mView;
    private ApiServer mApiServer;

    @Inject
    PendingPaymentOrderPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(PendingPaymentOrderContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadOrder(int orderStatus, int pageNum) {
        PurchaseOrderListReq req = new PurchaseOrderListReq();
        req.orderStatus = orderStatus;
        req.pageNum = pageNum;
        mApiServer.orderList(req)
                .compose(RxUtil.<PurchaseOrderListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<PurchaseOrderListResp>bindToLife())
                .subscribe(new BaseSubscriber<PurchaseOrderListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadOrderFailure();
                    }

                    @Override
                    public void onNext(PurchaseOrderListResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.loadOrderSuccess(resp);
                        } else {
                            mView.loadOrderFailure();
                        }
                    }
                });
    }

    @Override
    public void orderCancel(String id, final int position) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.orderCancel(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.orderCancelFailure("取消订单失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.orderCancelSuccess(resp, position);
                            } else {
                                mView.orderCancelFailure(resp.msg);
                            }
                        } else {
                            mView.orderCancelFailure("取消订单失败");
                        }
                    }
                });
    }

    @Override
    public void orderDelete(String id, final int position) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.orderDelete(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.orderDeleteFailure("删除订单失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.orderDeleteSuccess(resp, position);
                            } else {
                                mView.orderDeleteFailure(resp.msg);
                            }
                        } else {
                            mView.orderDeleteFailure("删除订单失败");
                        }
                    }
                });
    }

    @Override
    public void remind(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.orderRemind(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.orderRemindFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.orderRemindSuccess(resp);
                            } else {
                                mView.orderRemindFailure(resp.msg);
                            }
                        } else {
                            mView.orderRemindFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void confirm(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.orderConfirm(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.orderConfirmFailure("操作失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.orderConfirmSuccess(resp);
                            } else {
                                mView.orderConfirmFailure(resp.msg);
                            }
                        } else {
                            mView.orderConfirmFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void orderLogistics(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.orderLogistics(req)
                .compose(RxUtil.<LogisticsResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<LogisticsResp>bindToLife())
                .subscribe(new BaseSubscriber<LogisticsResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.orderLogisticsFailure("操作失败");
                    }

                    @Override
                    public void onNext(LogisticsResp resp) {
                        if (null != resp) {
                            if (resp.isOk()&& null != resp.data) {
                                mView.orderLogisticsSuccess(resp.data);
                            } else {
                                mView.orderLogisticsFailure(resp.msg);
                            }
                        } else {
                            mView.orderLogisticsFailure("操作失败");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("PendingPaymentOrderDetailPresenter").e("unSubscribe");
        mView = null;
    }
}
